<?php
namespace app\api\command;

use app\common\logic\UserExtend;
use rongyun\api\RongCloud;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class OffLine extends Command
{
    protected function configure()
    {
        $this->setName('offline')->setDescription('OffLine');
    }

    protected function execute(Input $input, Output $output)
    {
        /* 永不超时 */
        ini_set('max_execution_time', 0);
        $this->doCron();
    }

   
    public function doCron(){

        $cache = cache('command'.__CLASS__);
        if ($cache){
            echo 'running<br />';return false;
        }else{
            cache('command'.__CLASS__,true,60);
        }

        $model  = new RongCloud(config('rongyun')['appKey'],config('rongyun')['appSecret']);

        $userModel = new UserExtend();
        $time = time();

        $map['online_status'] = 1;
//        $map['online_time'] = ['<',$time - 7200];
        //查询在线用户  查看是否真实在线  否则改为离线
        Db::name('user_extend')
            ->where($map)
            ->field('user_id')
            ->chunk(200,function ($items)use ($model,$userModel,$time){
                $rows = [];

            foreach ($items as $k => $v){
                $result = $model->user()->checkOnline(hashid($v['user_id']));
                $array  = json_decode($result,true);

                if (is_zero($array['status'])){
                    $row['user_id']       = $v['user_id'];
                    $row['online_status'] = 0;
                    $row['online_time']   = $time;
                    $rows[] = $row;
                }
            }
            if (!empty($rows)){
               $userModel->saveAll($rows);
            }
        });

        cache('command'.__CLASS__,null);

    }
}